package com.jds.test.bio.p2;

/**
 * https://www.cnblogs.com/silyvin/articles/11970337.html
 * Created by joyce on 2019/11/26.
 */
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress(8000));
        System.out.println("Server startup at 8000");
        for (;;) {
            Socket socket = serverSocket.accept();
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();

            while (true) {
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String line = reader.readLine();
                    out.write((line + "\r\n").getBytes());
                }
                catch (Exception e) {
                    break;
                }
            }
        }
    }
}

